package date;

import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.util.*;

public class MyDate {
    public static void main(String[] args) {
        /**
         * LocalDate关注日期
         */
        LocalDate localDate = LocalDate.now();
        System.out.println("当前日期： " + localDate);

        System.out.println("==+" + LocalDate.now().getDayOfMonth());
        System.out.println("==+" + LocalDate.now().getDayOfWeek());
        System.out.println("==+" + LocalDate.now().getDayOfYear());

        String format = LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE);
        System.out.println("当前日期格式化：" + format);

        System.out.println("当前月份： " + localDate.getMonth());
        System.out.println("当前日期： " + localDate.getYear() + "," + localDate.getMonthValue() + "," + localDate.getDayOfMonth());

        LocalDate localDate1 = LocalDate.of(2020, 3, 24);
        System.out.println("构造任意日期： " + localDate1);

        /**
         * monthDay仅仅只关注月和日，不关注年
         */
        LocalDate localDate2 = LocalDate.of(2020, 8, 20);
        MonthDay monthDay = MonthDay.of(localDate2.getMonth(), localDate2.getDayOfMonth());
        MonthDay from = MonthDay.from(LocalDate.of(2010, 8, 20));
        if (monthDay.equals(from)) {
            System.out.println("今天是我生日");
        } else {
            System.out.println("今天不是我生日");
        }

        /**
         * 只关注年月
         */
        YearMonth yearMonth = YearMonth.of(2008, 2);
        System.out.println("2008年2月：" + yearMonth);
        System.out.println("2008年2月有多少天？" + yearMonth.lengthOfMonth());
        System.out.println("2008年是闰年吗？" + yearMonth.isLeapYear());

        /**
         * 日期加减
         */
        LocalDate after2Week = localDate.plus(2, ChronoUnit.WEEKS);
        System.out.println("当前日期增加两周：" + after2Week);
        LocalDate before2Week1 = localDate.plus(-2, ChronoUnit.WEEKS);
        LocalDate before2Week2 = localDate.minus(2, ChronoUnit.WEEKS);
        System.out.println("当前日期减去两周：" + before2Week1);
        System.out.println("当前日期减去两周：" + before2Week2);

        /**
         * 日期前后判断
         */
        System.out.println("日期前后判断：" + after2Week.isAfter(localDate));
        System.out.println("日期前后判断：" + localDate1.isBefore(localDate));

        System.out.println("当前时间和当前时间比较：" + LocalDate.now().isEqual(LocalDate.of(2021, 5, 13)));

        /**
         * 周期
         */
        LocalDate localDate3 = LocalDate.now();
        LocalDate localDate4 = LocalDate.of(2021, 3, 19);
        Period period = Period.between(localDate4, localDate3);
        System.out.println("这两个日期隔了多少年？多少月？多少天？" + period.getYears() + "年" + period.getMonths() + "月" + period.getDays() + "天");

        /**
         * 时区
         */
        Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();
        TreeSet<String> treeSet = new TreeSet<String>() { // 对时区set排序
            {
                addAll(availableZoneIds);
            }
        };
        System.out.println(treeSet);

        ZoneId zoneId = ZoneId.of("Asia/Shanghai");
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println("带时区的日期：" + localDateTime);
        ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, zoneId);
        System.out.println("带指定时区的日期：" + zonedDateTime);

        /**
         * Instant，不带UTC（时区）的日期
         */
        System.out.println(Instant.now());

        /**
         * LocaleTime关注时分秒
         */
        LocalTime localTime = LocalTime.now();
        System.out.println("当前时分秒：" + localTime);

        System.out.println("一个半小时后：" + localTime.plusHours(1).plusMinutes(30));

        /**
         * Clock系统时间
         */
        Clock clock = Clock.systemDefaultZone();
        System.out.println("当前时间毫秒数" + clock.millis());

        // LocalDate和Date进行转换
        System.out.println("LocalDate转换Date：=========");
        transferAB();
        System.out.println("Date转换LocalDate：=========");
        transferBA();

    }

    public static void transferAB() {
        Date date = new Date();
        Instant instant = date.toInstant();
        ZoneId sysZoneId = ZoneId.systemDefault();


        System.out.println("666:" + date.getHours() + date.getMinutes() + date.getSeconds());

        // atZone()方法返回在指定时区从此Instant生成的ZonedDateTime。
        LocalDate localDate = instant.atZone(sysZoneId).toLocalDate();
        System.out.println("Date = " + date);
        System.out.println("LocalDate = " + localDate);

        Calendar calendar = Calendar.getInstance();
        Random random = new Random();
        int i = random.nextInt(10000);

        String format = LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE);
        System.out.println(format + "-" + calendar.get(Calendar.HOUR_OF_DAY) + "" + calendar.get(Calendar.MINUTE) + "" + calendar.get(Calendar.SECOND) + "" + i);
    }

    public static void transferBA() {
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDate localDate = LocalDate.now();
        ZonedDateTime zdt = localDate.atStartOfDay(zoneId);

        Date date = Date.from(zdt.toInstant());

        System.out.println("LocalDate = " + localDate);
        System.out.println("Date = " + date);
    }
}
